﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyHocSinh.HeThongXuLy;
using QuanLyHocSinh.HeThongLuuTru;


namespace QuanLyHocSinh
{
    public partial class FormTiepNhanLopHoc : Form
    {
        LopHocDTO lhHienTai = new LopHocDTO();

        private static LopHocDTO LopHocDTO()
        {
            throw new NotImplementedException();
        }

        public FormTiepNhanLopHoc()
        {
            InitializeComponent();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            //Khoi tao doi tuong lop hoc
            LopHocDTO lh = new LopHocDTO();
            //doc thong tin cua lop hoc tu form
            lh.TenLop = txtTenLop.Text;
            lh.KhoiLop = int.Parse(txtKhoiLop.Text);
            lh.LopChuyen = txtLopChuyen.Text;
            lh.PhongHoc = txtPhongHoc.Text;
            //goi ham de them du lieu vao csdl
            if (LopHocDAO.ThemLopHoc(lh))
            {
                MessageBox.Show("Them lop hoc thanh cong", "Thong bao");
                HienThiDanhSachLopHoc();
            }
            else
            {
                MessageBox.Show("Them lop hoc that bai", "Thong bao");
            }
        }

        void HienThiDanhSachLopHoc()
        {
            // Doc CSDL tu CSDL
            DataTable ds = LopHocDAO.DanhSachLopHoc();
            // Hien thi danh sach len
            dgvDanhSachLop.DataSource = ds;
        }
        private void FormTiepNhanLopHoc_Load(object sender, EventArgs e)
        {
            HienThiDanhSachLopHoc();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvDanhSachLop_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                // Lay chi so cua dong dang duoc chon
                if (dgvDanhSachLop.SelectedCells.Count > 0)
                {
                    // Lay chi so cua dong dang duoc chon tu datagridview
                    int row = dgvDanhSachLop.SelectedCells[0].RowIndex;

                    // Lay lop hoc duoc chon tu datagridview ra
                    DongToiLopHocHienTai(dgvDanhSachLop.Rows[row]);

                    // Hien thi lop hoc len form
                    txtMaLop.Text = lhHienTai.MaLop.ToString();
                    txtTenLop.Text = lhHienTai.TenLop;
                    txtKhoiLop.Text = lhHienTai.KhoiLop.ToString();
                    txtLopChuyen.Text = lhHienTai.LopChuyen;
                    txtPhongHoc.Text = lhHienTai.PhongHoc;

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                
            }
        }

        void DongToiLopHocHienTai(DataGridViewRow row)
        {
            // Thu tu 0: ma lop, 1: ten lop, 2: khoi lop, 3: lop chuyen, 4: phong hoc
            // Tuy thuoc vao CSDl
            lhHienTai.MaLop = (int)row.Cells[0].Value; // ma lop
            lhHienTai.TenLop = (String)row.Cells[1].Value; // ten lop
            lhHienTai.KhoiLop = (int)row.Cells[2].Value; // khoi lop
            lhHienTai.LopChuyen = (String)row.Cells[3].Value; // Lop chuyen
            lhHienTai.PhongHoc = (String)row.Cells[4].Value; // ten lop
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            lhHienTai.TenLop = txtTenLop.Text;
            lhHienTai.KhoiLop = int.Parse(txtKhoiLop.Text);
            lhHienTai.LopChuyen = txtLopChuyen.Text;
            lhHienTai.PhongHoc = txtPhongHoc.Text;

            if (LopHocDAO.SuaLopHoc(lhHienTai))
            {
                MessageBox.Show("Sua Lop Hoc Thanh Cong", "Thong Bao");
                HienThiDanhSachLopHoc();

            }
            else
            {
                MessageBox.Show("Sua Lop Hoc Khong Duoc", "Thong Bao");
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (LopHocDAO.XoaLopHoc(lhHienTai))
            {
                MessageBox.Show("Xoa Lop Hoc Thanh Cong", "Thong Bao");
                HienThiDanhSachLopHoc();

            }
            else
            {
                MessageBox.Show("Xoa Lop Hoc Khong Duoc", "Thong Bao");
            }

        }

       
    }
}
